package org.example.jindie;

import cn.hutool.json.JSONUtil;

import java.util.ArrayList;
import java.util.HashMap;

public class Test2 {

    public static void main(String[] args) {

        String sql = "create table bd_account_jd\n" +
                "(\n" +
                "    bai_shu_id           int auto_increment comment '百数云系统id'\n" +
                "        primary key,\n" +
                "    bai_shu_create_time  datetime default CURRENT_TIMESTAMP null comment '百数云创建时间',\n" +
                "    FCURRENCYS           varchar(255)                       null comment '外币核算',\n" +
                "    FIsAllowRedistribute varchar(255)                       null comment '允许再分配',\n" +
                "    FDISTORGID           int                                null comment '分配组织',\n" +
                "    FHelperCode          int                                null comment '助记码',\n" +
                "    FAllCurrency         varchar(255)                       null comment '核算所有币别',\n" +
                "    FISCONTACT           varchar(255)                       null comment '往来科目',\n" +
                "    FModifierId          int                                null comment '修改人',\n" +
                "    FName                varchar(255)                       null comment '名称',\n" +
                "    FLevel               int                                null comment '级次',\n" +
                "    FIsSysPreset         varchar(255)                       null comment '系统预置',\n" +
                "    FAcctItemIsValid     varchar(255)                       null comment '是否启用',\n" +
                "    FISCASHFLOW          varchar(255)                       null comment '现金等价物',\n" +
                "    FIsBank              varchar(255)                       null comment '银行科目',\n" +
                "    FIsCash              varchar(255)                       null comment '现金科目',\n" +
                "    FUnitId              int                                null comment '默认单位',\n" +
                "    FContactsFlex        varchar(255)                       null comment '往来核算维度',\n" +
                "    FCFIndirectItemId    int                                null comment '附表项目(增加)',\n" +
                "    FNumber              varchar(255)                       null comment '编码',\n" +
                "    FUNITGROUPID         int                                null comment '计量单位组',\n" +
                "    FFlEXITEMPROPERTYID  int                                null comment '维度',\n" +
                "    FIsAllowAddChild     varchar(255)                       null comment '允许新增下级科目',\n" +
                "    FCurrencyName        varchar(255)                       null comment '币别名称',\n" +
                "    FDISTFORBIDDERID     int                                null comment '禁用人（分配）',\n" +
                "    FControlOrgId        int                                null comment '管控组织',\n" +
                "    FDISTFBDSTATUS       varchar(255)                       null comment '禁用状态（分配）',\n" +
                "    FDISTFORBIDDATE      varchar(255)                       null comment '禁用时间（分配）',\n" +
                "    FIsQty               varchar(255)                       null comment '启用数量金额辅助核算',\n" +
                "    FDocumentStatus      varchar(255)                       null comment '数据状态',\n" +
                "    FCurrencyList        varchar(255)                       null comment '外币核算(作废)',\n" +
                "    FISSHOWJOURNAL       varchar(255)                       null comment '出日记账',\n" +
                "    FParentID            int                                null comment '上级科目',\n" +
                "    FCurrencyID          int                                null comment '币别编码',\n" +
                "    FACCTTBLID           int                                null comment '科目表',\n" +
                "    FInputType           int                                null comment '必录类型',\n" +
                "    FOCFItemID           int                                null comment '主表项目(减少)',\n" +
                "    FFullName            varchar(255)                       null comment '全名',\n" +
                "    FDC                  int                                null comment '余额方向',\n" +
                "    FForbidDate          varchar(255)                       null comment '禁用日期',\n" +
                "    FIsInternalContacts  varchar(255)                       null comment '内部往来科目',\n" +
                "    FCFItemID            int                                null comment '主表项目(增加)',\n" +
                "    FISDETAIL            varchar(255)                       null comment '明细科目',\n" +
                "    FDISTFORBIDORGID     int                                null comment '禁用组织（分配）',\n" +
                "    FForbidderID         int                                null comment '禁用人',\n" +
                "    FITEMDETAILID        int                                null comment '核算维度',\n" +
                "    FUNMANUAL            varchar(255)                       null comment '不允许手工录入',\n" +
                "    FAMOUNTDC            int                                null comment '发生额方向',\n" +
                "    FCreateDate          datetime                           null comment '创建日期',\n" +
                "    FDISTUSEORGID        int                                null comment '使用组织',\n" +
                "    FAuditorID           int                                null comment '审核人',\n" +
                "    FDescription         varchar(255)                       null comment '描述',\n" +
                "    FDataFieldName       varchar(255)                       null comment '数据字段名称',\n" +
                "    FModifyDate          datetime                           null comment '修改日期',\n" +
                "    FGROUPID             int                                null comment '科目类别',\n" +
                "    FOCFIndirectItemId   int                                null comment '附表项目(减少)',\n" +
                "    FUseOrgId            int                                null comment '使用组织',\n" +
                "    FCreateOrgId         int                                null comment '创建组织',\n" +
                "    FIsAllocate          varchar(255)                       null comment '期末调汇',\n" +
                "    FCreatorId           int                                null comment '创建人',\n" +
                "    FForbidStatus        varchar(255)                       null comment '禁用状态',\n" +
                "    FAuditDate           datetime                           null comment '审核日期'\n" +
                ")\n" +
                "    comment '科目表';";

        //获取第一个左括号的位置
        int leftBracketIndex = sql.indexOf("(");
        //获取左有一个右括号的位置
        int rightBracketIndex = sql.lastIndexOf(")");
        //截取左括号和右括号中间的内容
        String content = sql.substring(leftBracketIndex + 1, rightBracketIndex);
        //将content 用 逗号分割
        String[] split = content.split(",");
        //遍历数组
        ArrayList<HashMap<String, String>> hashMaps = new ArrayList<>();
        for (int i = 2; i < split.length; i++) {
            //去掉换行符
            String s = split[i].replace("\n", "");
            //定位 null comment
            int commentIndex = s.indexOf("null comment");
            //删除 null comment和后面的内容
            String substring = s.substring(0, commentIndex);
            //定位 varchar/int/double/datetime/text 的位置
            int varcharIndex = substring.indexOf(" varchar");//找不到的话返回-1
            int intIndex = substring.indexOf(" int ");
            int doubleIndex = substring.indexOf(" double ");
            int datetimeIndex = substring.indexOf(" datetime ");
            int textIndex = substring.indexOf(" text ");
            //获取这5个位置的最大值
            int max = Math.max(Math.max(Math.max(Math.max(varcharIndex, intIndex), doubleIndex), datetimeIndex), textIndex);
            //获取字段名
            String fieldName = substring.substring(0, max).trim();
            //获取字段类型 max 到最后一个字符
            String fieldType = substring.substring(max).trim();
            if (fieldType.contains("varchar")) {
                fieldType = "varchar";
            }

//            System.out.println("字段名：" + fieldName + " 字段类型：" + fieldType);
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("name", fieldName);//{"name":"FId","type":"int"}
            hashMap.put("type", fieldType);
            hashMaps.add(hashMap);

        }

        System.out.println(JSONUtil.toJsonStr(hashMaps));


    }

}
